package com.example.freshmenclient.fragments;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.freshmenclient.AnouncenMentDetailsActivity;
import com.example.freshmenclient.LoginActivity;
import com.example.freshmenclient.R;
import com.example.freshmenclient.adapter.AnouncenmentsAdapter;
import com.example.freshmenclient.bean.AnouncenmentsBean;
import com.example.freshmenclient.util.GainDataUtil;
import com.example.freshmenclient.util.UrlUtil;
import com.google.gson.Gson;

import org.json.JSONObject;


public class AnouncenMentsFragment extends Fragment {

    private static final String TAG = "AnouncenMentsFragment";

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;
    private ListView lvAnouncements;
    private AnouncenmentsBean anouncenmentsBean;


    public AnouncenMentsFragment() {

    }


    public static AnouncenMentsFragment newInstance(String param1, String param2) {
        AnouncenMentsFragment fragment = new AnouncenMentsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        String url = UrlUtil.basePath + "freshmenserver/announcement/list";

      /*  RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        JsonObjectRequest request = new JsonObjectRequest(
                Request.Method.POST,
                url,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Gson gson = new Gson();
                        AnouncenmentsBean anouncenmentsBean = gson.fromJson(jsonObject.toString(), AnouncenmentsBean.class);
                        lvAnouncements.setAdapter(new AnouncenmentsAdapter(getContext(), anouncenmentsBean.getData()));
                        showAnouncenmentsDetail();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                }
        );
        requestQueue.add(request);*/


        GainDataUtil.gainDataForPost(getContext(), url, null
                , new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Gson gson = new Gson();
                        anouncenmentsBean = gson.fromJson(jsonObject.toString(), AnouncenmentsBean.class);
                        lvAnouncements.setAdapter(new AnouncenmentsAdapter(getContext(), anouncenmentsBean.getData()));
                        showAnouncenmentsDetail();
                    }
                }, null);


/*        JSONObject jsonObject = GainDataUtil.gainDataForPost(getContext(), url, null);
        Log.e(TAG, "onActivityCreated: " + jsonObject);

        if (jsonObject != null) {
            Gson gson = new Gson();
            AnouncenmentsBean anouncenmentsBean = gson.fromJson(jsonObject.toString(), AnouncenmentsBean.class);
            lvAnouncements.setAdapter(new AnouncenmentsAdapter(getContext(), anouncenmentsBean.getData()));
            showAnouncenmentsDetail();
        }*/

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_anouncen_ments, container, false);
        lvAnouncements = view.findViewById(R.id.lv_anouncements);
        return view;
    }

    private void showAnouncenmentsDetail() {
        lvAnouncements.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //保存公告Id
                UrlUtil.anouncenmentId = anouncenmentsBean.getData().get(position).getId();

                startActivity(new Intent(getContext(), AnouncenMentDetailsActivity.class));
            }
        });

    }

}
